<?php
namespace Admin\Controller;

class MenuController extends AdminController{

	public function index(){
		$pid  = I('get.pid',0);
        if($pid){
            $data = M('Menu')->where("id={$pid}")->field(true)->find();
            $this->assign('data',$data);
        }
        $title      =   trim(I('get.title'));
        $all_menu   =   M('Menu')->getField('id,title');
        $map['pid'] =   $pid;
        if($title)
            $map['title'] = array('like',"%{$title}%");
        $list       =   M("Menu")->where($map)->field(true)->order('sort asc,id asc')->select();
        if($list) {
            foreach($list as &$key){
                if($key['pid']){
                    $key['up_title'] = $all_menu[$key['pid']];
                }
            }
            $this->assign('list',$list);
        }

        $menus = M('Menu')->field(true)->select();
        $menus = D('Common/Tree')->toFormatTree($menus);
        $menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')), $menus);
        $this->assign('Menus', $menus);

        $this->meta_title = '菜单管理';
        $this->display();
	}

	public function add(){
		if(IS_AJAX){
			$menu = D('Menu');
            $data = $menu->create();
            if($data){
                if($insert_id = $menu->add()){
                    $msg_data['status']	= 1;
	    			$msg_data['msg']	= '新增成功';
					action_log('新增菜单',CONTROLLER_NAME,$insert_id,$this->c_admin_info['id'],$this->c_admin_info['user_login']);
	    			$this->ajaxReturn($msg_data);
                } else {
                    $msg_data['status']	= 0;
	    			$msg_data['msg']	= '新增失败';
	    			$this->ajaxReturn($msg_data);
                }
            } else {
                $msg_data['status']	= 0;
	    		$msg_data['msg']	= $menu->getError();
	    		$this->ajaxReturn($msg_data);
            }
		}else{
			$menus = M('Menu')->field(true)->select();
	        $menus = D('Common/Tree')->toFormatTree($menus);
	        $menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')), $menus);
	        $this->assign('Menus', $menus);

	        $this->meta_title = '新增菜单';
			$this->display();
		}
	}

	public function del(){
		$id = I('get.id',0);
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
		//查询是否有子菜单
		$menu = M('Menu');
		$iscat = $menu->where("pid=$id")->find();
		if($iscat){
			$this->error('请先删除此菜单下的子菜单');
		}else{
			$map = array('id' => $id );
	        if($menu->where($map)->delete()){
				action_log('删除菜单',CONTROLLER_NAME,$id,$this->c_admin_info['id'],$this->c_admin_info['user_login']);
	            $this->success('删除成功');
	        } else {
	            $this->error('删除失败！');
	        }
		}

	}


	public function edit(){
		if(IS_POST){
			$menu	= D('Menu');
			$data = $menu->create();
			if($data){
				if($menu->save()){
					action_log('更新菜单',CONTROLLER_NAME,0,$this->c_admin_info['id'],$this->c_admin_info['user_login']);
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}else{
				$this->error($menu->getError());
			}
		}else{
			$this->redirect('Menu/index');
		}
	}
}
?>
